﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AccManagerKw.Common;
using Model;
using Model.Interfaces;
using Utilities;

namespace AccManagerKw.Masters
{
    public partial class FrmUnitMaster : FrmBaseMaster, ICrud
    {
        private ClsCommonFn _oCommFn = new ClsCommonFn();
        private UnitMaster _row;
        public FrmUnitMaster()
        {
            InitializeComponent();
        }
        private void FrmUnitMaster_Shown(object sender, EventArgs e)
        {
            _oCommFn.GroupControl_Setting(gclMain);
            _oCommFn.GroupControl_Setting(gclOpen);
            _oCommFn.GridSetting(gvUnits, gcUnits);
        }
        public bool OperationSave()
        {
            try
            {
                if (IsValid())
                {
                    if ((Mode == ClsGlobalVarz.Mode.Add || Mode == ClsGlobalVarz.Mode.Edit) &&
                        _oCommFn.ValidateCrud(Mode))
                    {
                        OperationUnit();
                        _oCommFn.MsgBoxCrud(Mode);
                        return true;
                    }
                    else if (Mode != ClsGlobalVarz.Mode.Add && Mode != ClsGlobalVarz.Mode.Edit)
                    {
                        _oCommFn.WarningMsg("Check Mode");
                        return false;
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                _oCommFn.TryCatchErrorMsg(ex.Message);
                return false;
            }
        }

        private void OperationUnit()
        {
            using (var context = new AccManagerEntities())
            {
                var oUMaster = new UnitMaster();
                if (Mode == ClsGlobalVarz.Mode.Add)
                {
                    oUMaster.UnitId = context.UnitMasters.ToList().Count > 0
                        ? context.UnitMasters.Max(x => x.UnitId) + 1 : 1;
                }
                else if (Mode == ClsGlobalVarz.Mode.Edit)
                {
                    oUMaster = context.UnitMasters.Single(x => x.UnitId == _row.UnitId);
                }
                oUMaster.UnitCode = txtCode.Text;
                oUMaster.UnitName = txtUnitName.Text;

                if (Mode == ClsGlobalVarz.Mode.Add)
                    context.UnitMasters.Add(oUMaster);
                context.SaveChanges();
            }
        }

        public void OperationOpen()
        {
            var context = new AccManagerEntities();
            gcUnits.DataSource = context.UnitMasters.ToList();
            gclOpen.BringToFront();
        }
        private void gvUnits_DoubleClick(object sender, EventArgs e)
        {
            _row = (UnitMaster)gvUnits.GetFocusedRow();
            txtCode.Text = _row.UnitCode;
            txtUnitName.Text = _row.UnitName;

            _oCommFn.DisableControls(gclMain, true);
            gclMain.BringToFront();
        }


        public void OperationEdit()
        {
            if (_oCommFn.ValidateOpenRecord(_row.UnitId))
            {
                _oCommFn.DisableControls(gclMain, false);
                SetMode(ClsGlobalVarz.Mode.Edit);
            }
        }

        public bool OperationDelete()
        {
            try
            {
                if (_row != null && _oCommFn.ValidateOpenRecord(_row.UnitId))
                {
                    SetMode(ClsGlobalVarz.Mode.Delete);
                    if (_oCommFn.ValidateCrud(Mode))
                    {
                        using (var context = new AccManagerEntities())
                        {
                            var oUnitMaster = context.UnitMasters.Single(x => x.UnitId == _row.UnitId);
                            context.UnitMasters.Remove(oUnitMaster);
                            context.SaveChanges();
                            _oCommFn.MsgBoxCrud(Mode);
                        }
                        return true;
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                _oCommFn.TryCatchErrorMsg(ex.Message);
                return false;
            }
        }

        public void ResetData()
        {
            _oCommFn.ClearFields(this);
            _oCommFn.DisableControls(gclMain, false);
            gclMain.BringToFront();
            SetMode(ClsGlobalVarz.Mode.Add);
        }

        public bool IsValid()
        {
            ccDxValidationProvider1.Validate();
            return ccDxValidationProvider1.GetInvalidControls().Count <= 0;
        }



    }
}
